package vn.gq.tym.fragment;

import java.util.ArrayList;
import java.util.List;
import com.google.ads.AdView;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import vn.gq.tym.model.ChapterItemAdapter;
import vn.gq.tym.storytemplate.R;
import vn.gq.tym.storytemplate.StoryReaderActivity;
import vn.gq.tym.widget.tab.BaseFragment;
import vn.gq.udv.db.items.ChapterItem;
import vn.gq.udv.utils.AdmobAdsHandler;
import vn.gq.udv.utils.Bookmark;

public class ChapterFragment extends BaseFragment {

	private ListView mListView;
	private TextView chapterNotification;
	private List<ChapterItem> mChapterList = new ArrayList<ChapterItem>();
	private int mCatId = 0;
	private String mCatName = "";
	private Bookmark bookmark;
	private AdmobAdsHandler mAdHandler;
	private ChapterItemAdapter mAdapter;
	private ProgressBar mProgressBar;
	
	public static ChapterFragment newInstance(int catId, String catName) {
		ChapterFragment f = new ChapterFragment();
		Bundle args = new Bundle();
		args.putInt("CATID", catId);
		args.putString("CATNAME", catName);
		f.setArguments(args);
		return f;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle args = getArguments();
		if (args != null) {
			mCatName = args.getString("CATNAME");
			mCatId = args.getInt("CATID");
		} else {
			mCatId = 0;
			mCatName = "";
		}
	}
	
	@Override
	public int getContainerId() {
		return R.id.main_container;
	}

	@Override
	public int getLayoutId() {
		return R.layout.chapter_scr;
	}

	@Override
	public void createView() {
		mListView = (ListView) mLayout.findViewById(R.id.chapterlist);
		mListView.setVisibility(View.GONE);
		mProgressBar = (ProgressBar) mLayout.findViewById(R.id.progress);
		mProgressBar.setVisibility(View.VISIBLE);
		chapterNotification = (TextView) mLayout.findViewById(R.id.tvnotification);
		chapterNotification.setSelected(true);
		bookmark = new Bookmark(getActivity(), chapterNotification);
		mAdapter = new ChapterItemAdapter(getActivity());
		mAdapter.load(mCatId, new Runnable() {
			
			@Override
			public void run() {
				mListView.setAdapter(mAdapter);
				mListView.setVisibility(View.VISIBLE);
				mProgressBar.setVisibility(View.GONE);
				bookmark.setData(mAdapter.getData());
				mChapterList = mAdapter.getData();
			}
		});
		
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				if (id != -1) {
					ChapterItem chapter = mChapterList.get(position);
					chapter.setPosition(position);
					StoryReaderActivity.startActivity(getActivity(), (ArrayList<ChapterItem>) mChapterList, position);
				}
			}
		});
		
		mAdHandler = new AdmobAdsHandler((AdView) mLayout.findViewById(R.id.adView));
		mAdHandler.loadAds();
	}

	@Override
	public void onResume() {
		super.onResume();
		mAdHandler.loadAds();
		bookmark.showNotificationOnStartUp();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		mAdHandler.onDestroy();
	}

	@Override
	public void onStop() {
		super.onStop();
		mAdHandler.onStop();
	}

	

}
